$(function () {
    //CGJXFP 采购进项发票
    var cgjxfpColumns = [
        [
            {field: 'gsmc', title: '公司名称', width: 25, align: 'center'},
            {field: 'cgdh', title: '采购单号', width: 12, align: 'center'},
            {field: 'gysmc', title: '供应商', width: 25, align: 'center'},
            {field: 'code', title: '物资编码', width: 10, align: 'center'},
            {field: 'name', title: '品名', width: 10, align: 'center'},
            {field: 'ggxh', title: '规格', width: 10, align: 'center'},
            {field: 'th', title: '图号', width: 10, align: 'center'},
            {field: 'dw', title: '单位', width: 4, align: 'center'},
            {field: 'rksl', title: '入库数量', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'jshsj', title: '含税价', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'jsbhsj', title: '不含税价', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'jshse', title: '含税额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'jsse', title: '税额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'jsbhse', title: '净额', width: 8, align: 'center', formatter: formatSlForHtml2}
        ]
    ];

    //WLJXFP 物流进项发票
    var wljxfpColumns = [
        [
            {field: 'gsmc', title: '公司名称', width: 25, align: 'center'},
            {field: 'cgdh', title: '采购单号', width: 12, align: 'center'},
            {field: 'sydwmc', title: '物流商', width: 25, align: 'center'},
            {field: 'fymc', title: '费用名称', width: 12, align: 'center'},
            {field: 'jfsl', title: '计费数量', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'je', title: '计费金额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'hdje', title: '核定金额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'kdse', title: '可抵税额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'jsje', title: '净额', width: 8, align: 'center', formatter: formatSlForHtml2}
        ]
    ];

    //CGJXGS 采购进项估收
    var cgjxgsColumns = [
        [
            {field: 'gsmc', title: '公司名称', width: 25, align: 'center'},
            {field: 'cgdh', title: '采购单号', width: 12, align: 'center'},
            {field: 'gysmc', title: '供应商', width: 25, align: 'center'},
            {field: 'code', title: '物资编码', width: 10, align: 'center'},
            {field: 'name', title: '品名', width: 10, align: 'center'},
            {field: 'ggxh', title: '规格', width: 10, align: 'center'},
            {field: 'th', title: '图号', width: 10, align: 'center'},
            {field: 'dw', title: '单位', width: 4, align: 'center'},
            {field: 'rksl', title: '入库数量', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'hsj', title: '含税价', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'bhsj', title: '不含税价', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'hse', title: '含税额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'se', title: '税额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'bhse', title: '净额', width: 8, align: 'center', formatter: formatSlForHtml2}
        ]
    ];

    //WLJXGS物流进项估收
    var wljxgsColumns = [
        [
            {field: 'gsmc', title: '公司名称', width: 25, align: 'center'},
            {field: 'cgdh', title: '采购单号', width: 12, align: 'center'},
            {field: 'sydwmc', title: '物流商', width: 25, align: 'center'},
            {field: 'fymc', title: '费用名称', width: 10, align: 'center'},
            {field: 'jfsl', title: '计费数量', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'je', title: '计费金额', width: 8, align: 'center', formatter: formatSlForHtml2}
        ]
    ];

    //SLTZ_数量调整
    var sltzColumns = [
        [
            {field: 'kcpdbm', title: '库存盘点单号', width: 18, align: 'center'},
            {field: 'khbm', title: '卡号', width: 18, align: 'center'},
            {field: 'gsqc', title: '公司名称', width: 25, align: 'center'},
            {field: 'ckmc', title: '仓库名称', width: 15, align: 'center'},
            {field: 'code', title: '物资编码', width: 15, align: 'center'},
            {field: 'name', title: '品名', width: 10, align: 'center'},
            {field: 'ggxh', title: '规格', width: 15, align: 'center'},
            {field: 'th', title: '图号', width: 5, align: 'center'},
            {field: 'dw', title: '单位', width: 5, align: 'center'},
            {field: 'yksl', title: '盈亏数量', width: 15, align: 'center', formatter: formatSlForHtml},
            {field: 'ykje', title: '盈亏金额', width: 15, align: 'center', formatter: formatSlForHtml},
            {field: 'pdrq', title: '盘点时间', width: 10, align: 'center'},
            {field: 'pdrxm', title: '盘点人', width: 8, align: 'center'},
            {field: 'shrq', title: '审核时间', width: 10, align: 'center'},
            {field: 'shrxm', title: '审核人', width: 8, align: 'center'}
        ]
    ];

    //XSFP_销项发票
    var xsfpColumns = [
        [
            {field: 'gsmc', title: '公司名称', width: 25, align: 'center'},
            {field: 'xsdh', title: '销售单号', width: 12, align: 'center'},
            {field: 'khmc', title: '客户名称', width: 25, align: 'center'},
            {field: 'code', title: '物资编码', width: 10, align: 'center'},
            {field: 'name', title: '品名', width: 10, align: 'center'},
            {field: 'ggxh', title: '规格', width: 10, align: 'center'},
            {field: 'th', title: '图号', width: 10, align: 'center'},
            {field: 'dw', title: '单位', width: 10, align: 'center'},
            {field: 'cksl', title: '出库数量', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'xsdj', title: '销售单价', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'je', title: '净额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'se', title: '税额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'cbje', title: '成本金额', width: 8, align: 'center', formatter: formatSlForHtml2}
        ]
    ];

    //NBLL内部领料
    var nbllColumns = [
        [
            {field: 'lldh', title: '领料单号', width: 25, align: 'center'},
            {field: 'llgs', title: '领料单位', width: 25, align: 'center'},
            {field: 'llbm', title: '领料部门', width: 15, align: 'center'},
            {field: 'llr', title: '领料人', width: 10, align: 'center'},
            {field: 'ckmc', title: '仓库名称', width: 15, align: 'center'},
            {field: 'code', title: '物资编码', width: 10, align: 'center'},
            {field: 'name', title: '品名', width: 10, align: 'center'},
            {field: 'ggxh', title: '规格', width: 10, align: 'center'},
            {field: 'th', title: '图号', width: 10, align: 'center'},
            {field: 'dw', title: '单位', width: 4, align: 'center'},
            {field: 'flsl', title: '发料数量', width: 8, align: 'center', formatter: formatSlForHtml},
            {field: 'flje', title: '发料金额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'cbje', title: '成本金额', width: 8, align: 'center', formatter: formatSlForHtml2},
            {field: 'flgs', title: '发料单位', width: 25, align: 'center'},
            {field: 'flbm', title: '发料部门', width: 15, align: 'center'},
            {field: 'flr', title: '发料人', width: 10, align: 'center'},
            {field: 'zdr', title: '制单人', width: 10, align: 'center'}
        ]
    ];

    //===============================================
    $("#cwsfc_detail_toolbar [name='gsId']").gsjgtree(1);
    //计划开始时间
    $("#cwsfc_detail_toolbar [name='startDate']:text").datebox({
        required: true
    });
    //计划结束时间
    $("#cwsfc_detail_toolbar [name='endDate']").datebox({
        required: true
    });

    //CGJXFP 采购进项发票  WLJXFP物流进项发票 CGJXGS采购进项估收 WLJXGS物流进项估收 SLTZ 数量调整 XSFP_销项发票 NBLL内部领料
    var columns = null;
    var groupField = null;
    var groupFormatterFunction = null;
    var type = $("#cwsfc_detail_toolbar [name='type']").val();
    switch (type) {
        case "cgjxfp":
            columns = cgjxfpColumns;
            groupField = "cgdh";
            groupFormatterFunction = function (value, rows) {
                return '采购单号:' + value + ' - ' + rows.length + ' 条记录';
            };
            break;
        case "wljxfp":
            columns = wljxfpColumns;
            groupField = "cgdh";
            groupFormatterFunction = function (value, rows) {
                return '采购单号:' + value + ' - ' + rows.length + ' 条记录';
            };
            break;
        case "cgjxgs":
            columns = cgjxgsColumns;
            groupField = "cgdh";
            groupFormatterFunction = function (value, rows) {
                return '采购单号:' + value + ' - ' + rows.length + ' 条记录';
            };
            break;
        case "wljxgs":
            columns = wljxgsColumns;
            groupField = "cgdh";
            groupFormatterFunction = function (value, rows) {
                return '采购单号:' + value + ' - ' + rows.length + ' 条记录';
            };
            break;
        case "sltz":
            columns = sltzColumns;
            groupField = "kcpdbm";
            groupFormatterFunction = function (value, rows) {
                return '盘点单号:' + value + ' - ' + rows.length + ' 条记录';
            };
            break;
        case "xsfp":
            columns = xsfpColumns;
            groupField = "xsdh";
            groupFormatterFunction = function (value, rows) {
                return '销售单号:' + value + ' - ' + rows.length + ' 条记录';
            };
            break;
        case "nbll":
            columns = nbllColumns;
            groupField = "lldh";
            groupFormatterFunction = function (value, rows) {
                return '发料单号:' + value + ' - ' + rows.length + ' 条记录';
            };
            break;
        default:
            columns = [
                []
            ];
    }

    //表格初始加载
    var grid = $('#cwsfc_detail_grid').datagrid({
        url: $.webPath() + "/report/cwsfc/detailList",
        toolbar: "#cwsfc_detail_toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        rownumbers: true,
        pageNumber: 1,
        striped: true,
        view: groupview,
        groupField: groupField,
        groupFormatter: groupFormatterFunction,
        showFooter: true,
        queryParams: {
            startDate: $("#cwsfc_detail_toolbar [name='startDate']").val(),
            endDate: $("#cwsfc_detail_toolbar [name='endDate']").val(),
            type: $("#cwsfc_detail_toolbar [name='type']").val(),
            gsId: $("#cwsfc_detail_toolbar [name='gsId']").val(),
            codeId: $("#cwsfc_detail_toolbar [name='codeId']").val()
        },
        columns: columns
    });

    //搜索按钮
    $("#cwsfc_detail_toolbar [name='searchButton']").click(function () {
        $(grid).datagrid({
            queryParams: {
                startDate: $("#cwsfc_detail_toolbar [name='startDate']").val(),
                endDate: $("#cwsfc_detail_toolbar [name='endDate']").val(),
                type: $("#cwsfc_detail_toolbar [name='type']").val(),
                gsId: $("#cwsfc_detail_toolbar [name='gsId']").val(),
                codeId: $("#cwsfc_detail_toolbar [name='codeId']").val()
            }});
    });
    //导出
    $("#cwsfc_detail_toolbar [name='export']").click(function () {
        window.open($.webPath() + "/report/cwsfc/exportDetail?"
            + "startDate=" + $("#cwsfc_detail_toolbar [name='startDate']").val()
            + "&endDate=" + $("#cwsfc_detail_toolbar [name='endDate']").val()
            + "&type=" + $("#cwsfc_detail_toolbar [name='type']").val()
            + "&gsId=" + $("#cwsfc_detail_toolbar [name='gsId']").val()
            + "&codeId=" + $("#cwsfc_detail_toolbar [name='codeId']").val());
    });
});